﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using BussinesLogic;

namespace Foliatura
{
    public partial class AdminUsuarios : Form
    {
        Controller controlador;
        ArrayList usuarios;
        bool isEdit;
        bool isNuevo;
        Usuario selectedUser;

        public AdminUsuarios()
        {
            this.controlador = Controller.Instance();
            this.usuarios = new ArrayList();
            InitializeComponent();

            this.isEdit = false;
            this.isNuevo = false;
            fillData();
            fillUserdata();
        }


        private void fillData()
        {
            this.usuarios = Usuario.all();

            this.lstUsuarios.DataSource = this.usuarios;
            this.lstUsuarios.DisplayMember = "Username";
        }

        private void fillUserdata()
        {
            this.selectedUser =
                (Usuario)this.lstUsuarios.SelectedItem;

            if (this.selectedUser != null)
            {
                this.txtApellido1.Text = this.selectedUser.Apellido1;
                this.txtApellido2.Text = this.selectedUser.Apellido2;
                this.txtDepartamento.Text = this.selectedUser.Departamento;
                this.txtNombre1.Text = this.selectedUser.Nombre1;
                this.txtNombre2.Text = this.selectedUser.Nombre2;
                this.txtNombreUsuario.Text = this.selectedUser.Username;
                this.chkAdministrador.Checked = this.selectedUser.Administrador;
                disableAll();
                this.isNuevo = false;
            }
        }

        private void disableAll()
        {
            this.txtApellido1.Enabled = false;
            this.txtApellido2.Enabled = false;
            this.txtDepartamento.Enabled = false;
            this.txtNombre1.Enabled = false;
            this.txtNombre2.Enabled = false;
            this.txtNombreUsuario.Enabled = false;
            this.txtPass1.Enabled = false;
            this.txtPass2.Enabled = false;
            this.chkAdministrador.Enabled = false;
        }

        private void enableAll()
        {
            this.txtApellido1.Enabled = true;
            this.txtApellido2.Enabled = true;
            this.txtDepartamento.Enabled = true;
            this.txtNombre1.Enabled = true;
            this.txtNombre2.Enabled = true;
            this.txtNombreUsuario.Enabled = true;
            this.txtPass1.Enabled = true;
            this.txtPass2.Enabled = true;
            this.chkAdministrador.Enabled = true;
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            this.isEdit = !this.isEdit;
            if (this.isEdit)
            {
                this.btnEditar.Text = "Cancelar";
                this.btnGuardar.Enabled = true;
                enableAll();
            }
            else
            {
                this.btnEditar.Text = "Editar";
                this.btnGuardar.Enabled = false;
                fillUserdata();
            }
        }

        private void clearAllData()
        {
            this.txtApellido1.Text = "";
            this.txtApellido2.Text = "";
            this.txtDepartamento.Text = "";
            this.txtNombre1.Text = "";
            this.txtNombre2.Text = "";
            this.txtNombreUsuario.Text = "";
            this.txtPass1.Text = "";
            this.txtPass2.Text = "";
            this.chkAdministrador.Enabled = false;
        }

        private void updateUserData()
        {
            this.selectedUser.Apellido1 = this.txtApellido1.Text;
            this.selectedUser.Apellido2 = this.txtApellido2.Text;
            this.selectedUser.Departamento = this.txtDepartamento.Text;
            this.selectedUser.Nombre1 = this.txtNombre1.Text;
            this.selectedUser.Nombre2 = this.txtNombre2.Text;
            this.selectedUser.Username = this.txtNombreUsuario.Text;
            this.selectedUser.Administrador = this.chkAdministrador.Checked;
            this.selectedUser.Password = this.txtPass1.Text;
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            clearAllData();
            enableAll();
            this.lstUsuarios.SelectedIndex = -1;
            this.isNuevo = true;
            this.btnGuardar.Enabled = true;
            this.selectedUser = new Usuario();
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            if (this.isNuevo)
            {
                updateUserData();
                try
                {
                    this.selectedUser.Create();
                    fillData();
                    fillUserdata();
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
            }
        }

        private void lstUsuarios_SelectedIndexChanged(object sender, EventArgs e)
        {
            fillUserdata();
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            controlador.ApplicationClose();
        }
    }
}
